<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {

	public function index() {
		// $ctl = $this->router->fetch_class();
		// $method = $this->router->fetch_method();
		// $module = $this->uri->segment(1);

		// $this->load->library('auth');
		// $status = $this->auth->check('admin/a', $uid);
		// var_dump($status);
		// exit;


		$this->load->view('admin/index.html', $this->_data);
	}


	public function copy() {
		$this->load->view('admin/copy.html');
	}

	/**
	 * 个人设置，修改密码
	 * @return [type] [description]
	 */
	public function profile() {
		$this->load->view('admin/profile.html');
	}

		/**
	 * 用户管理
	 * @return [type] [description]
	 */
	public function manage_admin() {
		//调取所有用户
		$this->load->model('admin_model', 'admin');
		$this->load->model('auth_model', 'auth');
		$users = $this->admin->get_admin();
		$roles = $this->auth->get_roles();

		$uid = $this->uri->segment(4);

		$this->_data['users'] = $users;
		$this->_data['roles'] = $roles;

		$uid = $this->uri->segment(4);

		if (false == $uid) {
			$this->_add_admin();
		} else {
			$this->_edit_admin($uid);
		}

		$this->load->view('admin/manage_admin.html', $this->_data);

	}

	private function _add_admin() {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('admin');

		if (false !== $status) {
			$passwd = md5($this->input->post('passwd'));
			$repasswd = md5($this->input->post('repasswd'));
			if ($passwd !== $repasswd) {
				error('密码不一致');
			}
			$data = array(
				'username' => $this->input->post('username'),
				'gid' => $this->input->post('gid'),
				'password' => $passwd
				);
			$this->admin->add_user($data);
			redirect('admin/admin/manage_admin');
		}
	}

	private function _edit_admin($uid) {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('admin');

		if (false == $status) {
			$this->_data['admin'] = $this->admin->get_admin($uid);
		} else {
			$passwd = md5($this->input->post('passwd'));
			$repasswd = md5($this->input->post('repasswd'));
			if ($passwd !== $repasswd) {
				error('密码不一致');
			}

			$data = array(
				'username' => $this->input->post('username'),
				'gid' => $this->input->post('gid'),
				'password' => $passwd
				);
			$this->admin->edit_user($uid, $data);
			$group_access = array(
				'group_id' => $this->input->post('gid')
				);
			$this->admin->edit_group_access($uid, $group_access);
			redirect('admin/admin/manage_admin');
		}
	}

	private function _get_group_by_id($gid) {
		$data = $this->auth->get_role($gid);
		if (!isset($data['title'])) {
			return '';
		}
		return $data['title'];
	}


}

/* End of file admin.php */
/* Location: ./application/controllers/admin/admin.php */